<?php
namespace app\index\controller;
use app\common\controller\Homebase;

class System extends Homebase
{
    public function import(){
        if (!$this->request->isPost()) {
            $datalist = Db('import')->select();
            $this->assign('datalist',$datalist);
            return $this->fetch();  
        }else{
            $data = $this->request->param();
            Db('import')->where('id','<',100)->update(['status'=>0,'is_text'=>0]);
            foreach ($data as $key => $val) {
                list($where,$save) = explode('-',$key);
                $val=$val?$val:0;
                Db('import')->where('value',$where)->update([$save=>$val]);
            }
            return array("data"=>null,"success"=>true,"errorCode"=>0,"errorMsg"=>"");
        }
    }

    public function msg(){
        $datasytem = Db('msg')->where('type',0)->select();
        $dataauto = Db('msg')->where('type',1)->select();
        $this->assign('datasytem',$datasytem);
        $this->assign('dataauto',$dataauto);
        return $this->fetch();
    }

    public function addmsg(){
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['content'] = trim($data['content']);
            $data['type'] = 1;
            Db('msg')->insert($data);
            return array("data"=>null,"success"=>true,"errorCode"=>0,"errorMsg"=>"添加成功");
        }else{
            return $this->fetch();
        }
    }

    public function editmsg(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['content'] = trim($data['content']);
            Db('msg')->update($data);
            return array("data"=>null,"success"=>true,"errorCode"=>0,"errorMsg"=>"添加成功");
        }else{
            $id = $this->request->param('id');
            $data = Db('msg')->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }
    
    public function edit(){
        if ($this->request->isPost()) {
            $data = $this->request->param();
            Db('msg')->update($data);
            return array("data"=>null,"success"=>true,"errorCode"=>0,"errorMsg"=>"添加成功");
        }else{
            $id = $this->request->param('id');
            $data = Db('msg')->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    public function tags(){
        $datalist = Db('tags')->select();
        $this->assign('datalist',$datalist);
        return $this->fetch();
    }

    public function tagsedit(){
        if (!$this->request->isPost()) {
            $id = $this->request->param('id');
            $data = Db('tags')->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();          
        }else{
            $data = $this->request->param();
            Db('tags')->update($data);
            return array("data"=>null,"success"=>true,"errorCode"=>0,"errorMsg"=>"添加成功");
        }
    }

}
